package com.hnichr.ump.talent.library.provider.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hnichr.ump.common.schedule.dto.RunResultDTO;
import com.hnichr.ump.common.talent.library.entity.EducationInfo;
import com.hnichr.ump.common.talent.library.entity.PersonalInfo;
import com.hnichr.ump.common.talent.library.entity.Resume;
import com.hnichr.ump.common.talent.library.entity.SchoolList;
import com.hnichr.ump.common.talent.library.handle.SchoolTypeHandle;
import com.hnichr.ump.common.talent.library.service.EducationInfoService;
import com.hnichr.ump.common.talent.library.service.PersonalInfoService;
import com.hnichr.ump.common.talent.library.service.ResumeService;
import com.hnichr.ump.common.talent.library.service.SchoolListService;
import com.hnichr.ump.talent.library.provider.SyncPolicyProvider;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class ResumeSyncPolicyProvider implements SyncPolicyProvider {
    private static final String KEY = "resumeList";

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private SchoolListService schoolListService;

    @Autowired
    private EducationInfoService educationInfoService;

    @Autowired
    private PersonalInfoService personalInfoService;

    @Override
    public boolean support(JSONObject jsonObject) {
        return jsonObject.containsKey(KEY);
    }

    @Override
    @Transactional
    public void sync(String syncDate, JSONObject jsonObject) {
        JSONArray resumeList = jsonObject.getJSONArray(KEY);
        if(CollectionUtils.isEmpty(resumeList)){
            return;
        }
        for(Object resumeObject:resumeList){
            if(resumeObject instanceof JSONObject){
                JSONObject resumeJSONObject = (JSONObject) resumeObject;
                Resume resume = new Resume();
                resume.setId(resumeJSONObject.getString("id"));
                resume.setDelFlag(resumeJSONObject.getBoolean("delFlag"));
                resume.setUserId(resumeJSONObject.getString("userId"));
                if(resumeJSONObject.containsKey("createTime")){
                    resume.setOrginalCreateTime(new Date(resumeJSONObject.getLong("createTime")));
                }
                if(resumeJSONObject.containsKey("updateTime")){
                    resume.setOrginalUpdateTime(new Date(resumeJSONObject.getLong("updateTime")));
                }

                resume.setResumeName(resumeJSONObject.getString("resumeName"));
                resume.setType(resumeJSONObject.getInteger("type"));
                resume.setResumeType(resumeJSONObject.getInteger("resumeType"));
                resume.setAdvantage(resumeJSONObject.getString("advantage"));
                resume.setMajorResearchDirection(resumeJSONObject.getString("majorResearchDirection"));
                resume.setSelfEvaluation(resumeJSONObject.getString("selfEvaluation"));
                resume.setOtherSituations(resumeJSONObject.getString("otherSituations"));
                resume.setMajorWorkPerformance(resumeJSONObject.getString("majorWorkPerformance"));
                resume.setTripartiteAgreement(resumeJSONObject.getString("tripartiteAgreement"));
                resume.setLanType(resumeJSONObject.getInteger("lanType"));
                resume.setSyncDate(syncDate);
                resumeService.merge(resume);
            }
        }
    }

    @Override
    @Transactional
    public void postSync(String syncDate){
        List<Resume> resumeList = resumeService.getBySyncDate(syncDate);

        if(CollectionUtils.isEmpty(resumeList)){
            return;
        }
        List<SchoolList> schoolLists = schoolListService.listAll();
        SchoolTypeHandle handle = new SchoolTypeHandle();
        handle.init(schoolLists);

        for(Resume resume:resumeList){
            //学校级别，985  211  世界前200
            List<EducationInfo> educationInfoList = educationInfoService.getListByResumeId(resume.getId());
            String schoolType = handle.classify(educationInfoList);
            resume.setSchoolType(schoolType);

            //personal是否河南人
            PersonalInfo personalInfo = personalInfoService.getByUserId(resume.getUserId());
            if(personalInfo!=null){
                if(StringUtils.startsWith(personalInfo.getHomeTown(),"410000")){
                    resume.setHeNanRen("1");
                }
                else{
                    resume.setHeNanRen("0");
                }
                resume.setName(personalInfo.getName());
            }

            resumeService.update(resume);
        }
    }

}
